package com.tang.hawkmusic.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tang.hawkmusic.R;
import com.tang.hawkmusic.bean.music.LocalMusic;

import java.util.ArrayList;

public class LocalMusicAdapter extends RecyclerView.Adapter<LocalMusicAdapter.MusicViewHolder> {
    
    private ArrayList<LocalMusic> localMusics;
    private Activity activity;
    public LocalMusicAdapter(ArrayList<LocalMusic> localMusics, Activity activity) {
        this.localMusics = localMusics;
        this.activity=activity;
    }

    @NonNull
    @Override
    public LocalMusicAdapter.MusicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_local_music,parent,false);
        MusicViewHolder holder=new MusicViewHolder(view);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull LocalMusicAdapter.MusicViewHolder holder, int position) {
          LocalMusic music=localMusics.get(position);
          holder.name.setText(music.getName());
          holder.fullName.setText(music.getSinger());
          holder.clickBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent intent=new Intent();
                  intent.putExtra("position",position);
                  intent.putExtra("musics",localMusics);
                  activity.setResult(2,intent);
                  activity.finish();
              }
          });

    }

    @Override
    public int getItemCount() {
        return localMusics.size();
    }

    public class MusicViewHolder extends RecyclerView.ViewHolder {
        public ImageView playImg,moreBtn;
        public TextView name,fullName;
        ImageView clickBtn;
        public MusicViewHolder(@NonNull View itemView) {
            super(itemView);
            playImg=itemView.findViewById(R.id.play_img);
            moreBtn=itemView.findViewById(R.id.local_moreBtn);
            name= itemView.findViewById(R.id.local_music_name);
            fullName=itemView.findViewById(R.id.local_music_full_name);
            clickBtn=itemView.findViewById(R.id.music_clickBtn);
        }
    }

}
